package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

//Purely for demo purposes
//Will probably not be used at all later on

public class HexDemoPanel extends JPanel {

	public Camera camera = new Camera();
	private int xNum; // how many columns of hexes
	private int yNum; // how many rows of hexes
	private int size; // radius of hex
	private TexturePaint texture; // an image to paint hexes with
	private BufferedImage hexGrid; // an image that holds the hexes

	public HexDemoPanel(int x, int y) {
		this.xNum = x;
		this.yNum = y;
		size = 50;
		hexGrid = new BufferedImage((xNum + 2) * size * 2,
				(int) ((yNum + 2) * Math.sqrt(3) / 2) * size * 2, 
				BufferedImage.TYPE_INT_RGB);
		
		// reads in an image to use as a texture
		try {
			BufferedImage img = ImageIO.read(
					new File("assets/images/buttonImages/hover/" +
							"offensive_stance.png"));
			texture = new TexturePaint(img,
					new Rectangle2D.Double(0, 0,
							img.getWidth(), img.getHeight()));
		}
		catch(IOException e) {

		}
		makeHex();
	}

	public void gameLoop() {
		while(true) {
			repaint();
		}
	}
	
	public void makeHex() {
		Graphics g = hexGrid.getGraphics();
		Graphics2D graphics = (Graphics2D) g;
		
		//set up graphics settings
		graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
			RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		for(int x = 0; x < xNum; x++)
			for(int y = 0; y < yNum; y++) {
				double tempY = 0;
				if(x%2==1) {
					tempY = Math.sqrt(3) / 2.0 * size;
				}
				GeneralPath path = new GeneralPath();
				path.moveTo(x * size * 1.5,
						(y * 2 + 1) * size * (Math.sqrt(3) / 2.0) + tempY);
				path.lineTo(x * size * 1.5 + .5 * size,
						y * 2 * size * (Math.sqrt(3) / 2.0) + tempY);
				path.lineTo(x * size * 1.5 + 1.5 * size,
						y * 2 * size * (Math.sqrt(3) / 2.0) + tempY);
				path.lineTo(x * size * 1.5 + 2 * size,
						(y * 2 + 1) * size * (Math.sqrt(3) / 2.0) + tempY);
				path.lineTo(x * size * 1.5 + 1.5 * size,
						(y * 2 + 2) * size * (Math.sqrt(3) / 2.0) + tempY);
				path.lineTo(x * size * 1.5 + .5 * size,
						(y * 2 + 2) * size * (Math.sqrt(3) / 2.0) + tempY);
				path.closePath();

				graphics.setPaint(texture);
				graphics.fill(path);
				graphics.setColor(Color.BLACK);
				graphics.draw(path);
			}
	}
	
	public void paint(Graphics g) {
		
		if (MouseInfo.getPointerInfo().getLocation().x < 50)
			camera.scrollLeft();
		if(MouseInfo.getPointerInfo().getLocation().x > this.getWidth() - 50)
			camera.scrollRight();
		if (MouseInfo.getPointerInfo().getLocation().y < 50)
			camera.scrollUp();
		if(MouseInfo.getPointerInfo().getLocation().y > this.getHeight() - 50)
			camera.scrollDown();
		
		g.translate(camera.getXOffset(), camera.getYOffset());
		g.drawImage(hexGrid, 0, 0, this);
		
	}

}
